package com.fescoservice.service;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fescoservice.dao.BBSDAO;
import com.fescoservice.dao.CategoryDAO;
import com.fescoservice.dao.CategoryImageDAO;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.BBS;
import com.fescoservice.model.Category;
import com.fescoservice.model.CategoryId;
import com.fescoservice.model.CategoryImage;
import com.fescoservice.model.CategoryImageId;

@Service
public class CategoryService{
	private static final Logger log = LogManager.getLogger(CategoryService.class);
	@Autowired
	private CategoryDAO categoryDAO;
	@Autowired
	private CategoryImageDAO categoryImgDAO;
	
	public void saveCategoryImage(CategoryImage image){
		
		categoryImgDAO.save(image);
	}	

	public void saveCategory(Category transientInstance){
		categoryDAO.save(transientInstance);
	}	

	public void updateCategory(Category transientInstance){
		categoryDAO.update(transientInstance);
	}	
	
	public void deleteCategory(Category category) throws FSException{		
		String hql = " c where c.id.boardCode1 = :boardCode1";
		if(!"FM0000".equals(category.getId().getBoardCode2())){
			hql+=" and c.id.boardCode2 = :boardCode2";
		}
		if(!"FB0000".equals(category.getId().getBoardCode3())){
			hql+=" and c.id.boardCode3 = :boardCode3";
		}
		categoryDAO.delCategories(" delete from Category"+hql,category.getId().getBoardCode1(),category.getId().getBoardCode2(),category.getId().getBoardCode3());					
		categoryDAO.delCategories(" delete from CategoryImage"+hql,category.getId().getBoardCode1(),category.getId().getBoardCode2(),category.getId().getBoardCode3());					
		categoryDAO.delCategories(" delete from BBS"+hql,category.getId().getBoardCode1(),category.getId().getBoardCode2(),category.getId().getBoardCode3());					
		categoryDAO.delCategories(" delete from BBSContent"+hql,category.getId().getBoardCode1(),category.getId().getBoardCode2(),category.getId().getBoardCode3());					
		categoryDAO.delCategories(" delete from BBSAttach"+hql,category.getId().getBoardCode1(),category.getId().getBoardCode2(),category.getId().getBoardCode3());				
	}	
	
	public Category findCategoryById(CategoryId id) {
		return categoryDAO.findById(id);
	}
	
	public Long countCategories(String language)  throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_CATEGORY C WHERE C.LOCALE = '"+language+"'";
		return categoryDAO.countCategories(sql);
	}
	
	public Long countCategoryImages(CategoryImageId id)  throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_CATEGORY_IMAGE C WHERE C.BOARD_CODE1 = :boardCode1 AND C.BOARD_CODE2 = :boardCode2 AND C.BOARD_CODE3 = :boardCode3";
		return categoryImgDAO.countCategoryImages(sql,id);
	}
	
	public List getCode1Names(String language) throws FSException{
		String hql = "from Category c where c.locale = :language and c.id.boardCode2 = :boardCode2 and c.id.boardCode3 = :boardCode3";
		return categoryDAO.getCode1Names(hql,language);
	}
	
	public List getCode2Names(String language,String boardcode1) throws FSException{
		String hql = "from Category c where c.locale = :language and c.id.boardCode1 = :boardCode1 and c.id.boardCode3 = :boardCode3";
		return categoryDAO.getCode2Names(hql,language,boardcode1);
	}
	
	public List<Category> getAllCategories(String language){
		Category category = new Category();
		category.setLocale(language);
		return categoryDAO.findByExample(category);
	}
	
	public Long countCode1Name(String boardCode1) throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_CATEGORY C WHERE C.BOARD_CODE1 ='"+boardCode1+"'";// ?";
		return categoryDAO.countCode1Name(sql,boardCode1);
	}
	
}
